package com.fire.control.system;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.corundumstudio.socketio.SocketIOServer;
import com.fire.control.system.config.getui.GetuiManager;
import com.fire.control.system.config.quartz.QuartzManager;
import com.fire.control.system.config.quartz.TestJob;
import com.fire.control.system.config.redis.RedisService;
import com.fire.control.system.config.socketIo.SocketIoMessageEventHandler;
import com.fire.control.system.dto.GetuiDto;
import com.fire.control.system.dto.MqttDev;
import com.fire.control.system.module.common.controller.BaseController;
import com.fire.control.system.module.company.service.CompanyService;
import com.fire.control.system.module.warning.service.WarningService;
import com.fire.control.system.support.util.XdaoDate;

@RestController
@RequestMapping("/test")
public class TestController extends BaseController{
	
	//@Autowired private PubData dataPublish;
	@Autowired RedisService redisService;
	@Autowired WarningService warningService;
	@Autowired QuartzManager quartzManager;
	@Autowired GetuiManager getuiManager;
	@Autowired private SocketIOServer socketIOServer;  
	@Autowired private SocketIoMessageEventHandler socket;
	@Autowired private CompanyService companyService;
	
	@RequestMapping("getChildCompanyIdList")
	public List<String> getChildCompanyIdList(String companyId){
		return companyService.getChildCompanyIdList(companyId);
	}
	
	
	@RequestMapping("sendSocket/{clientId}/{msg}")
	public void sendSocket(@PathVariable String clientId,@PathVariable String msg){
		/*SocketMessageInfo info = new SocketMessageInfo();
		info.setMessage(msg);
        socketIOServer.getClient(UUID.fromString(sessionId)).sendEvent("messageevent", info);*/
		socket.send(clientId, msg);
	}
	
	@RequestMapping("/sendMsg")
	public String testSendMsg(String msg){
		//dataPublish.sendToMqtt(msg);
		return msg;
	}
	
	@RequestMapping("/getui")
	public GetuiDto getui(){
		//GetuiDto dto = getuiManager.pushLinkSingle("火灾报警", "测试推送!", "64631c0e4671f77dfaa075896465f437","http://www.baidu.com");
		GetuiDto dto = getuiManager.pushNotificationSingle("火灾报警", "测试通知!", "64631c0e4671f77dfaa075896465f437");
		return dto;
	}
	
	@RequestMapping("/session")
	public void session(HttpServletRequest request){
		//System.out.println(LocalDateTime.now()+ "---"+request.getSession().getId()+"---"+request.getSession().getMaxInactiveInterval());
		//request.getSession().setAttribute(Key.session_key_lora_user_id.toString(), new User("123"));
		System.out.println(getUser().getUserId()+"==="+getUser().getCompany().getId());
	}
	
	@RequestMapping("/redis")
	public void redis(){
		/*Demo demo = new Demo();
		demo.setDemoName("新闻");
		redisService.setObject("demo", demo);
		demo = redisService.getObject("demo", Demo.class);
		System.out.println(demo);
		System.out.println(demo.getDemoName());
		redisService.expire("demo", 0);*/
		//redisService.setObject(Key.redis_key_lora_user_id_+"1", new User("123"));
	}
	
	@RequestMapping("mqtt")
	public void testMqtt(){
		MqttDev dev = new MqttDev();
		dev.setDevEUI("device1");
		dev.setfPort("19");
		//warningService.changeWarningProtocol(dev, "压力报警压力高于上线");
	}
	
	@RequestMapping(path="quartz",method=RequestMethod.POST)
	public boolean execute(String jobName,String time){
		if(StringUtils.isNotEmpty(jobName) && StringUtils.isNotEmpty(time)){
			return quartzManager.execute(jobName,TestJob.class, XdaoDate.dateToCron(time));			
		}
		return false;
	}
	
	@RequestMapping("quartz/cron")
	public boolean cron(String jobName,String cron){
		return quartzManager.execute(jobName, TestJob.class, cron);
	}
	
	@RequestMapping("quartz/removeJob/{jobName}")
	public boolean removeJob(@PathVariable("jobName")String jobName) {
		return quartzManager.removeJob(jobName);
	}
	
	@RequestMapping("shutdownJobs")
	public boolean shutdownJobs(){
		return quartzManager.shutdownJobs();
	}
	
}
